import 'package:eshi/app/CommonFunctions.dart';
import 'package:eshi/app/shi/data/System.dart';
import 'package:eshi/app/shi/widget/WebTextWidget.dart';
import 'package:flutter/material.dart';

class PrivacyPolicyPage extends StatelessWidget {
  const PrivacyPolicyPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: mText('隐私政策'),
      ),
      body: Center(
        child: mText('这里是隐私政策的具体内容'),
      ),
    );
  }
}

class PrivacyPolicyDialog extends StatefulWidget {
  // 回调函数：处理用户选择（同意/拒绝）
  final Function(bool) onResult;

  const PrivacyPolicyDialog({
    super.key,
    required this.onResult,
  });

  @override
  State<PrivacyPolicyDialog> createState() => _PrivacyPolicyDialogState();
}

class _PrivacyPolicyDialogState extends State<PrivacyPolicyDialog> {
  // 记录用户是否已滚动到文档底部
  bool _hasScrolledToEnd = true;
  late ScrollController _scrollController;
  @override
  void initState() {
    super.initState();
    _scrollController = ScrollController();
    // 监听滚动是否结束
    _scrollController.addListener((){
        // 滚动结束时触发
        if(!_hasScrolledToEnd){
           final isAtBottom = _scrollController.position.maxScrollExtent -  _scrollController.offset <= 10;
           if(isAtBottom) {
             setState(() {
             _hasScrolledToEnd = isAtBottom;
           });
           }
        }
    });
  }
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      // 返回true允许关闭，false阻止关闭
      onWillPop: () async => false,
      child:Dialog(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(12),
        ),
        insetPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 24),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            // 标题区域
            Padding(
              padding: EdgeInsets.all(16),
              child: mText(
                '隐私政策',
                style: TextStyle(
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                  // color: Colors.black87,
                ),
              ),
            ),

            // 分隔线
            const Divider(height: 1, thickness: 1),

            // 政策内容（可滚动）
            Expanded(
              child:Container(
                color:  Colors.white54,
                child:  Padding(
                  padding: const EdgeInsets.all(16),
                  child:WebView("assets/doc/eshiPrivacyPolicy.html") 
                  // SingleChildScrollView(
                  //   controller: _scrollController,
                  //   child:  Expanded(child: WebView("assets/doc/eshiPrivacyPolicy.html")),
                  // ),
                ),
              ),
            ),

            // 按钮区域
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
              child: Row(
                children: [
                  // 拒绝按钮
                  Expanded(
                    child: ElevatedButton(
                      style: ElevatedButton.styleFrom(
                        // backgroundColor: Colors.grey[200],
                        foregroundColor: Colors.black87,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(8),
                        ),
                      ),
                      onPressed: () {
                        widget.onResult(false);
                        System.quit();
                      },
                      child:  Padding(
                        padding: EdgeInsets.symmetric(vertical: 12),
                        child: mText('拒绝'),
                      ),
                    ),
                  ),
                  const SizedBox(width: 12),
                  // 同意按钮（未滚动到底部时禁用）
                  Expanded(
                    child: ElevatedButton(
                      style: ElevatedButton.styleFrom(
                        backgroundColor: Colors.blue,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(8),
                        ),
                      ),
                      onPressed: _hasScrolledToEnd
                          ? () {
                              // 记录用户已同意
                              widget.onResult(true);
                              Navigator.pop(context);
                            }
                          : null,
                      child:  Padding(
                        padding: EdgeInsets.symmetric(vertical: 12),
                        child: mText('同意'),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      )
    );
  }

}
